﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using Gift.DAL;
using Gift.Model;

namespace WebSite.cn
{
    public partial class hotpro : BasePage
    {
        // Fields

     
     
        public string sLoginMessage = "&nbsp;温馨提示：请登录并充值后，选取您要兑换的商品！</br>";
        private static readonly Regex RX = new Regex("&amp;page=d+", RegexOptions.Compiled);
        readonly DALGoods theGoods = new DALGoods();
        readonly DALPointsArea thePointsArea = new DALPointsArea();
        public StringBuilder sProductHtml = new StringBuilder();
        readonly DALGoodsType theGoodsType = new DALGoodsType();
        tbGoodsType oGoodsType;

        string sLeftMenuFormat =  " <dl class=\"pl10\"> "
                                  + "  <dt class=\"thirdBg pl10 {0}\">{1}</dt><dd class=\"pl10 {0}\">"
                                  +" {2} "
                                   +" </dd> "
                                  +"  </dl> ";
        string sLeftMenuSubFormat = " <span><a  href=\"{0}\" target=\"_self\"  >{1}</a></span> ";
                

  

    


        public string ArrowDirectionSale
        {
            get
            {
                if (this.Context.Session["ArrowDirectionSale"] == null)
                {
                    return "&darr;";
                }
                else
                {
                    return this.Context.Session["ArrowDirectionSale"].ToString();
                }
            }
            set
            {
                this.Context.Session["ArrowDirectionSale"] = value;
            }
        }

        public string ArrowDirectionPrice
        {
            get
            {
                if (this.Context.Session["ArrowDirectionPrice"] == null)
                {
                    return "&darr;";
                }
                else
                {
                    return this.Context.Session["ArrowDirectionPrice"].ToString();
                }
            }
            set
            {
                this.Context.Session["ArrowDirectionPrice"] = value;
            }
        }
        string _leftTitleName = "";

        public string LeftTitleName
        {
            get { return _leftTitleName; }
          
        }

      public  string NavigaterName
        {
            get
            {
                try
                {
                    StringBuilder sbHtml = new StringBuilder();
                    if (string.IsNullOrEmpty(Request.QueryString["tid"]))
                    {
                        return "所有商品";
                    }
                    oGoodsType = theGoodsType.GetModel(int.Parse(Request.QueryString["tid"]));
                    string sTypeId = "";
  
                    if (oGoodsType != null)
                    {
                        sTypeId = oGoodsType.typeId.ToString();
                        _leftTitleName = oGoodsType.typeName;
                       sbHtml.AppendFormat("<a href=\"/cn/hotpro.html?tid={0}\">{1}</a>", oGoodsType.typeId.ToString(), oGoodsType.typeName);
                    }
                    if (!string.IsNullOrEmpty(Request.QueryString["subid"]))
                    {
                        oGoodsType = theGoodsType.GetModel(int.Parse(Request.QueryString["subid"]));
                        if (oGoodsType != null)
                        {
                            sbHtml.AppendFormat("><a href=\"/cn/hotpro.html?tid={0}&subid={1}\">{2}</a>",sTypeId, oGoodsType.typeId.ToString(), oGoodsType.typeName);
                        }
                    }
                    return sbHtml.ToString();
                }
                catch
                {
                    return string.Empty;
                }
            }
        }

      public   string GetBuilderOutPageHeaderHtml()
        {
        
                string input = this.Context.Request.Url.Query.Replace("?", "&amp;");
                input = RX.Replace(input, string.Empty);
                input = input.Replace("&amp;order=sale", string.Empty);
                input = input.Replace("&amp;order=price", string.Empty);
                input = input.Replace("&amp;order=time", string.Empty);
                string sLiClass1 = string.Empty;
                string sLiClass2 = string.Empty;
                string sLiClass3 = string.Empty;
                string sArrow1 = string.Empty;
                string sArrow2 = string.Empty;
                if (string.IsNullOrEmpty(this.Context.Request["order"]) || this.Context.Request["order"] == "sale")
                {
                    if (!string.IsNullOrEmpty(this.Context.Request["order"]))
                    {
                        if (ArrowDirectionSale == "&darr;")
                        {
                            ArrowDirectionSale = "&uarr;";
                        }
                        else
                        {
                            ArrowDirectionSale = "&darr;";
                        }
                    }
                    sArrow1 = ArrowDirectionSale;
                    sLiClass1 = "class='currentPage'";
                    sArrow2 = string.Empty;
                }
                else if (this.Context.Request["order"] == "price")
                {
                    if (ArrowDirectionPrice == "&darr;")
                    {
                        ArrowDirectionPrice = "&uarr;";
                    }
                    else
                    {
                        ArrowDirectionPrice = "&darr;";
                    }
                    sArrow2 = ArrowDirectionPrice;
                    sArrow1 = string.Empty;
                    sLiClass2 = "class='currentPage'";
                }
                else if (this.Context.Request["order"] == "time")
                {
                    sLiClass3 = "class='currentPage'";
                }
                StringBuilder sbPageHeader = new StringBuilder();
              //  sbPageHeader.Append("  <div style=\" width:100%; text-align:left;  height:19px; padding-left:5px; float:left\">");
                sbPageHeader.Append("  <div class='search_text'>排序</div>");
                sbPageHeader.Append("   <div id=\"orderMenu\">");
                sbPageHeader.Append("  <ul>");
                sbPageHeader.Append(string.Format("  <li {1}><a href=?order=sale{0}>{4}销  量</a></li><li {2}><a href=?order=price{0}>{5}福  点</a></li><li {3}><a href=?order=time{0}>上价时间</a></li>", input, sLiClass1, sLiClass2, sLiClass3, sArrow1, sArrow2));
                sbPageHeader.Append("  </ul>");
                sbPageHeader.Append("  </div>");

              //  sbPageHeader.Append(" </div>");

                return sbPageHeader.ToString();
                //System.Web.UI.HtmlControls.HtmlGenericControl divHtml = new System.Web.UI.HtmlControls.HtmlGenericControl();
                //divHtml.InnerHtml = sbPageHeader.ToString();
         
        }

        // Methods
        private void BindDataList()
        {
  

            int iCardtypeId = -1;

            int? iPointsFrom = 0;
            int? iPointsTo = 0;
            if (this.txtPointsFrom.Value != "")
            {
                iPointsFrom = int.Parse(this.txtPointsFrom.Value);
            }
            if (this.txtPointsTo.Value != "")
            {
                iPointsTo = int.Parse(this.txtPointsTo.Value);
            }
            if (this.txtPointsFrom.Value == "" && this.txtPointsTo.Value == "" && ! string.IsNullOrEmpty(Request.QueryString["points"]))
            {
                tbPointsArea oPointsArea = thePointsArea.GetModel(int.Parse(Request.QueryString["points"]));
                if (oPointsArea != null)
                {
                    iPointsFrom = oPointsArea.pointsFrom;
                    iPointsTo = oPointsArea.pointsTo;
                }
            }
            int iTermsId = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["terms"]))
            {
                if (int.TryParse(Request.QueryString["terms"], out iTermsId))
                {
                    iTermsId = int.Parse(Request.QueryString["terms"]);
                }

            }

            int iBrandId = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["bid"]))
            {
                 if(int.TryParse(Request.QueryString["bid"],out iBrandId))
                {
                    iBrandId = int.Parse(Request.QueryString["bid"]);
                }

            }

            DataSet ds = theGoods.GetDataListPage(base.Request.QueryString["tid"]
                                                 , Request.QueryString["subid"]
                                                 , Request.QueryString["order"]
                                                 , ArrowDirectionSale
                                                 , ArrowDirectionPrice
                                                 , this.AspNetPager1.StartRecordIndex 
                                                 , this.AspNetPager1.StartRecordIndex + this.AspNetPager1.PageSize
                                                 , iCardtypeId
                                                 , string.Empty
                                                 , this.txtKeywords.Text.Trim() == "" ?  Request.QueryString["key"] : this.txtKeywords.Text.Trim()
                                                 , false
                                                 , iTermsId
                                                 , iPointsFrom
                                                 , iPointsTo
                                                 , iBrandId
                                                 );


            if (ds != null)
            {
               
                AspNetPager1.RecordCount = int.Parse(ds.Tables[1].Rows[0][0].ToString());
                BindProductList(ds.Tables[0]);
            }


          
        }

        void BindProductList(DataTable dtProduct)
        {
            sProductHtml = new StringBuilder();
            if (dtProduct == null || dtProduct.Rows.Count <= 0) return;

            string sExchangeInstruction = "";

            sProductHtml.Append("<div class=\"info_list\"><ul>");
            string sProName = string.Empty;
            foreach (DataRow dr in dtProduct.Rows)
            {
                sExchangeInstruction = "";
                if (int.Parse(dr["cardTypeCount"].ToString()) > 0)
                {
                    sExchangeInstruction = "提货券可兑&nbsp;&nbsp;&nbsp;";
                }
                if (dr["allowAllCard"] != null &&  bool.Parse(dr["allowAllCard"].ToString()))
                {
                    sExchangeInstruction += "通用券可兑";
                }
                sProName = dr["goodsName"].ToString().Length > 12 ? dr["goodsName"].ToString().Substring(0, 12) + ".." : dr["goodsName"].ToString();
                sProductHtml.AppendFormat("<li>"
                                + " <a href=\"{0}\" target=\"_blank\" name=\"#hoverinfo{1}\" class=\"pro_img\"><img width=\"140\" height=\"140\" alt=\"\" src=\"{2}\" /></a>"
                                + "<div class='goods_info_inst'> <p><a href=\"{0}\"  title=\"{7}\">{3}</a></p><p><span class='fudian_count'>{5}</span><em>福点</em></p><span ><a href=\"{0}\"  title=\"{7}\"><img src='/cn/images/goods_exchange.png' alt=''/></a></span>"
                                + " <div id=\"hoverinfo{1}\" style=\"display: none;\"><img width=\"200\" height=\"200\" src=\"{2}\" alt=\"{7}\"/><p><strong>{7}</strong> <span>产品批号:{6}</span> <span><em>库存：{4}件</em></span><span><em>兑换价：{5}福点</em></span></p></div>"
                                + "</li>"
                                , "goodsDetail.html?gid=" + dr["goodsId"].ToString() + "&tid=" + Request.QueryString["tid"]
                                , dr["goodsId"].ToString()
                                , "/uploadfile/product/" + dr["bigPic"].ToString()
                                , sProName
                                , dr["goodsCount"].ToString()
                                ,  dr["changePoints"].ToString()
                                , dr["goodsCode"].ToString()
                                , dr["goodsName"].ToString()
                                );
            }

            sProductHtml.Append("</ul></div>");//<div class=\"clear\"></div></div><script type=\"text/javascript\">    $(function () { $(\"a.pro_img\").tooltip({ bodyHandler: function () { return $($(this).attr(\"name\")).html(); }, showURL: false }); });</script></textarea><div style=\"height:203px;\" class=\"div_lazyload_32\"></div></div></div>");
        }




        public void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if ((base.Request.QueryString["sid"] != null) && ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)))
            {
                Label label = (Label)e.Item.FindControl("price");
                Label label2 = (Label)e.Item.FindControl("Label1");
                Image image = (Image)e.Item.FindControl("Image1");
                if ((label.Text == "0") || (label.Text == "0.00"))
                {
                    label.Text = label2.Text;
                }
                if (image.ImageUrl.ToString() == "")
                {
                    image.ImageUrl = "../uploadfile/banner/" + info.info1("", "shopmopic");
                }
                else
                {
                    image.ImageUrl = "../uploadfile/product/" + image.ImageUrl.ToString();
                }
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            Session["backHref"] = Request.Url.PathAndQuery;
            if (!base.IsPostBack )
            {
            
             
              //  sPriceName = base.Request.QueryString["sid"] == "2" ? "兑换价":"零售价";
               this.AspNetPager1.CurrentPageIndex = 1;
            //    BindDataList(1);
            }
        }

  

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
          
            this.BindDataList();

        }

        public string GetLeftMenuHtml()
        {
            StringBuilder sbWriterMenuHtml = new StringBuilder();
            StringBuilder sbSubHtml = new StringBuilder();
            StringBuilder sbMenuHtml = new StringBuilder();
       
                DataTable tbLevelAll = new DataTable();
                if (string.IsNullOrEmpty(Request.QueryString["tid"]))
                {
                    tbLevelAll = theGoodsType.GetMainGoodsTypeList().Tables[0];
                }
                else
                {
                    tbLevelAll = theGoodsType.GetList(" typeId=" + Request.QueryString["tid"]).Tables[0];
                }
                foreach (DataRow drTop in tbLevelAll.Rows)
                {

                    DataTable table = theGoodsType.GetListByParentId(int.Parse(drTop["typeId"].ToString()), 0).Tables[0];
                    DataTable tableSub;
                    bool isSelectSubMenu = false;
                    if (table != null)
                    {
                        foreach (DataRow dr in table.Rows)
                        {
                            isSelectSubMenu = false;
                            tableSub = theGoodsType.GetListByParentId(int.Parse(dr["typeId"].ToString()), 1).Tables[0];
                            if (tableSub != null)
                            {
                                sbSubHtml = new StringBuilder();
                                foreach (DataRow drSub in tableSub.Rows)
                                {
                                    if (Request.QueryString["subid"] == drSub["typeId"].ToString())
                                    {
                                        isSelectSubMenu = true;
                                    }
                                    sbSubHtml.AppendFormat(sLeftMenuSubFormat
                                                          , "/cn/hotpro.html?tid=" + Request.QueryString["tid"] + "&subid=" + drSub["typeId"].ToString()
                                                          , drSub["typeName"].ToString()
                                                          );

                                }
                            }
                            if (Request.QueryString["subid"] == dr["typeId"].ToString())
                            {
                                isSelectSubMenu = true;
                            }


                            sbMenuHtml.AppendFormat(sLeftMenuFormat
                                                     , isSelectSubMenu ? "foc" : string.Empty
                                                    , dr["typeName"].ToString()
                                                    , sbSubHtml.ToString()
                                   
                                                    );
                        }
                    }
                    sbWriterMenuHtml.AppendFormat("<div class='big_class_title'>{0}</div>"
                                                     , drTop["typeName"].ToString());
                    sbWriterMenuHtml.Append(sbMenuHtml.ToString());
                    
                
            }
                return sbWriterMenuHtml.ToString();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
    
            this.BindDataList();
        }




    }
}